package com.css.zfzx.sjcj.modules.jobschedule.controller;

import com.css.bpm.platform.base.response.RestResponse;
import com.css.bpm.platform.constants.PlatformConstants;
import com.css.bpm.platform.utils.PlatformObjectUtils;
import com.css.zfzx.sjcj.common.RestResultData;
import com.css.zfzx.sjcj.modules.jobschedule.entity.po.JobScheduleReportPO;
import com.css.zfzx.sjcj.modules.jobschedule.entity.vo.JobScheduleReportVO;
import com.css.zfzx.sjcj.modules.jobschedule.entity.vo.QueryRespondVO;
import com.css.zfzx.sjcj.modules.jobschedule.service.JobScheduleReportService;
import com.css.zfzx.sjcj.modules.jobschedule.service.impl.JobScheduleReportExcelImportHelper;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional;

/**
 * <p>地震行业普查任务完成情况进度统计表</p>
 *
 * @author liuhy
 * @date 2021/8/17
 */
@RestController
@RequestMapping("/pc/jobSchedule")
public class JobScheduleController {

    private final JobScheduleReportService jobScheduleReportService;
    private final JobScheduleReportExcelImportHelper excelImportHelper;

    @Autowired
    public JobScheduleController(JobScheduleReportService jobScheduleReportService, JobScheduleReportExcelImportHelper excelImportHelper) {
        this.jobScheduleReportService = jobScheduleReportService;
        this.excelImportHelper = excelImportHelper;
    }

    @GetMapping("/find")
    public RestResultData<List<QueryRespondVO>> pcFindData(HttpServletRequest req) {
        String usersId = (String) req.getSession().getAttribute(PlatformConstants.CUR_USER_ID_IN_SESSION);
        List<QueryRespondVO> pcSchedule = jobScheduleReportService.findPcSchedule(usersId);
        return RestResultData.ok(pcSchedule);
    }

    @PostMapping("/pcScheduleData")
    public RestResultData pcScheduleData(HttpServletRequest req){
        String usersId = (String) req.getSession().getAttribute(PlatformConstants.CUR_USER_ID_IN_SESSION);
        jobScheduleReportService.httpsSendCommonMethod(usersId);
        RestResultData restResultData= new RestResultData();
        restResultData.setMessage("操作完成！");
        return RestResultData.ok(restResultData);
    }

    @PostMapping("/update")
    public RestResultData pcUpdateData(@RequestBody List<JobScheduleReportVO> vos, HttpServletRequest req) {
        String usersId = (String) req.getSession().getAttribute(PlatformConstants.CUR_USER_ID_IN_SESSION);
        jobScheduleReportService.updatePcSchedule(vos, usersId);
        RestResultData restResultData= new RestResultData();
        restResultData.setMessage("操作完成！");
        return RestResultData.ok(restResultData);
    }

    @PostMapping("/importExcel")
    public RestResponse<RestResponse<String>> importExcel(@RequestParam("file") MultipartFile file) {
        try {
            Optional<XSSFSheet> sheetOptional = excelImportHelper.getSheet(file.getInputStream());
            if (sheetOptional.isPresent()) {
                XSSFSheet sheet = sheetOptional.get();
                List<String> taskNameList = excelImportHelper.getTaskNameList(sheet);
                List<JobScheduleReportPO> data = excelImportHelper.getData(sheet, taskNameList);
                StringBuilder sb = new StringBuilder();
                data.stream()
                        .filter(d -> PlatformObjectUtils.isNotEmpty(d.getPercentage()) && Double.parseDouble(d.getPercentage()) > 100)
                        .forEach(e -> sb.append(e.getDivisionName()).append(" - ").append(e.getTaskName()).append(" 比例值不允许大于100").append("<br />"));
                if (sb.length() != 0)
                    return RestResponse.fail(sb.toString());
                jobScheduleReportService.saveAll(data);
            }
            RestResponse<String> success = RestResponse.succeed();
            success.setMessage("导入成功");
            return RestResponse.succeed(success);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.fail("导入失败!");
        }
    }

}
